//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.telnet.io;

import java.awt.Color;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TextAreaHandler extends Handler {
    private JTextArea textArea = new JTextArea();

    public TextAreaHandler() {
    }

    public JTextArea getTextArea() {
        this.textArea.setBackground(Color.white);
        this.textArea.setForeground(Color.black);
        this.textArea.setEditable(false);
        return this.textArea;
    }

    public void close() throws SecurityException {
    }

    public void flush() {
        this.textArea.setText("");
    }

    public void publish(final LogRecord record) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Date date = new Date(record.getMillis());
                DateFormat formatter = new SimpleDateFormat("MMM d, yyyy h:mm:ss a");
                String dateFormatted = formatter.format(date);
                StringWriter text = new StringWriter();
                PrintWriter out = new PrintWriter(text);
                out.print(TextAreaHandler.this.textArea.getText());
                out.printf("[%s] %s: %s.%s [Thread %d]\n      -> %s\n", record.getLevel(), dateFormatted, record.getSourceClassName(), record.getSourceMethodName(), record.getThreadID(), record.getMessage());
                TextAreaHandler.this.textArea.setText(text.toString());
            }
        });
    }
}
